package com.compayname.projectname.controller;

import com.compayname.projectname.base.data.ViewData;
import com.compayname.projectname.base.exception.DataDuplicateException;
import com.compayname.projectname.data.UserRequ;
import com.compayname.projectname.data.UserResp;
import com.compayname.projectname.pojo.User;
import com.compayname.projectname.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping(value = "/test")
public class HelloController {

    private static Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    public UserService service;


    /**
     * 页面返回
     */
    @RequestMapping(value="/hello1")
    public String home(Model model){
        System.out.println("redirect to hello page!");
        logger.debug("====redirect to hello page!");
        logger.info("====redirect to hello page!");
        logger.error("====redirect to hello page!");
        String name = "Mr.Liu";
        model.addAttribute("name", name);
        return "hello";
    }

    @RequestMapping(value="/hello2")
    @ResponseBody
    public ModelAndView goHome(){
        System.out.println("go to hello page!");
        ModelAndView mode = new ModelAndView();
        mode.addObject("name", "zhangsan");
        mode.setViewName("hello");
        return mode;
    }

    /**
     * 数据返回
     */
    @RequestMapping(value="/index")
    @ResponseBody
    public String index(){
        System.out.println("ResponseBody String test!");
        User user = service.findById(12304);
        return user.toString();
    }

    @RequestMapping(value="/index2")
    @ResponseBody
    public User index2(){
        System.out.println("ResponseBody JSON test!");
        User user = service.findById(12304);
        return user;
    }

    @RequestMapping(value="/index3")
    @ResponseBody
    public ViewData<UserResp> index3(){
        System.out.println("ResponseBody Package test!");
        User user = service.findById(12304);
        UserResp resp = new UserResp();
        resp.setUserId(user.getUserId());
        resp.setUserCode(user.getUserCode());
        resp.setUserName(user.getUserName());
        return ViewData.ok(resp);
    }

    @RequestMapping(value="/pagehelper")
    @ResponseBody
    public ViewData<List<User>> pagehelper(){
        System.out.println("ResponseBody pagehelper test!");
        List<User> userList = service.findAll();
        return ViewData.ok(userList);
    }

    @RequestMapping(value="/parameter1/{id}")
    @ResponseBody
    public ViewData<UserResp> parameter1(@PathVariable("id") Integer id){
        System.out.println("ResponseBody parameter1 test!");
        User user = service.findById(id);
        UserResp resp = new UserResp();
        resp.setUserId(user.getUserId());
        resp.setUserCode(user.getUserCode());
        resp.setUserName(user.getUserName());
        return ViewData.ok(resp);
    }

    @RequestMapping(value="/parameter2")
    @ResponseBody
    public ViewData<UserResp> parameter2(@RequestBody UserRequ userRequ){
        System.out.println("ResponseBody parameter2 & exception test!");
        if (userRequ.getUserId() == -1)
            return ViewData.error("请输入正确用户ID");
        if ("error".equals(userRequ.getUserCode())) {
            try {
                service.throwsExceptionWay();
            } catch (DataDuplicateException e) {
                return ViewData.error(-1, e.getMessage());
            }
        }
        UserResp resp = new UserResp();
        try {
            User user = service.findById(userRequ.getUserId());
            resp.setUserId(user.getUserId());
            resp.setUserCode(user.getUserCode());
            resp.setUserName(user.getUserName());
        } catch (Exception e) {
//            return ViewData.error(-1, e.getMessage());
            return ViewData.error(-1, e.toString());
        }
        return ViewData.ok(resp);
    }

}
